package Servlet;


import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
public class JDBCutil {
    private static DataSource ds;
    //读取配置文件，加载文件中的信息
    static {
        try {
            Properties pro=new Properties();
            InputStream is = JDBCutil.class.getClassLoader()
            .getResourceAsStream("druid.properties");
            pro.load(is);
          ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取连接
    public static Connection getConnection(){
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
     //释放资源DQL
    public static  void close(ResultSet rs, Statement stat,Connection conn){
    if (rs!=null){
        try {
            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
        if (stat!=null){
            try {
                stat.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //释放资源dml
    public static void close(Statement stmt, Connection conn) {
        close(null,stmt,conn);
    }

}
